package org.domain.sigepac.converter;

import java.text.ParseException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.swing.text.MaskFormatter;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

/**
 * Conversor para campos do tipo CPF.
 *
 * @author marcelo.guimaraes
 */
@Name("conversorCPF")
@BypassInterceptors
@org.jboss.seam.annotations.faces.Converter
public class ConversorCPF extends ConversorMascara implements Converter {

	@Override
	protected String mascara() {
		return "###.###.###-##";
	}

	@Override
  public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
    if(value == null) {
      return null;
    } else if(value.toString().trim().equals("")) {
      return null;
    }
    try {
      MaskFormatter formatter = new MaskFormatter(mascara());
      formatter.setValueContainsLiteralCharacters(false);
      String valor = value.toString().replaceAll("\\.", "");
      return formatter.valueToString(valor);
    } catch (ParseException e) {
    	e.printStackTrace();
      throw new ConverterException(e);
    }
  }

}
